﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Xpo;
using TaiLieu;
using System.IO;

public partial class tl_menuLeft : System.Web.UI.UserControl
{
    public string titl = "";
    public int parent = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        parent = ConvertType.ToInt(QueryHelper.Get_Value_TheEndOfQueryString(Request));
        string query = QueryHelper.Get_Value(Request, 1);
        switch (query)
        {
            case "trac-nghiem":
                titl = " trắc nghiệm";
                Treeview_TN_category();
                break;
            case "tai-lieu":
                titl = " tài liệu";
                Treeview_TaiLieu_download();
                break;
            case "gioi-thieu":
                titl = " giới thiệu";
                Treeview_data1();
                break;
            default:
                Treeview_data1();
                break;
        }
    }
    #region TaiLieu_download
    private void Treeview_TaiLieu_download()
    {
        string query = QueryHelper.Get_Value(Request, 1);
        ICollection<TaiLieu_download> list_TaiLieu_download = TaiLieu_downloadDB.GetChildrenByQueryParent(false, query, 0);
        foreach (TaiLieu_download item in list_TaiLieu_download)
        {
            TreeNode node = new TreeNode();
            node.Text = "&nbsp;" + item.Name;
            //string temp = path + dir.Name + "\\";
            node.Value = item.query;//this.ReplacePath(path) + dir.Name + "/";
            this.GetManyNode_TaiLieu_download(node, item.Id, query);
            this.TreeView1.Nodes.Add(node);
        }
    }
    protected void GetManyNode_TaiLieu_download(TreeNode node, int parent, string query)
    {
        try
        {
            ICollection<TaiLieu_download> tls = TaiLieu_downloadDB.GetChildrenByQueryParent(false, query, parent);
            foreach (TaiLieu_download tl in tls)
            {
                TreeNode n = new TreeNode();
                n.Text = "&nbsp;" + tl.Name;
                n.Value = tl.query;
                node.ChildNodes.Add(n);
            }
        }
        catch { }
    }

    #endregion
    #region TN_category
    private void Treeview_TN_category()
    {
        string query = QueryHelper.Get_Value(Request, 1);
        ICollection<TN_category> list_TN_category = TN_categoryDB.GetChildrenByQueryParent(false, query, 0);
        foreach (TN_category item in list_TN_category)
        {
            TreeNode node = new TreeNode();
            node.Text = "&nbsp;" + item.Name;
            node.Value = item.query;
            this.GetManyNode_TN_category(node, item.Id, query);
            //node.Expand();
            this.TreeView1.Nodes.Add(node);
        }
    }
    protected void GetManyNode_TN_category(TreeNode node, int parent, string query)
    {
        try
        {
            ICollection<TN_category> tls = TN_categoryDB.GetChildrenByQueryParent(false, query, parent);
            foreach (TN_category tl in tls)
            {
                TreeNode n = new TreeNode();
                //n.Text = "<a style=\"text-decoration:none\" href=\"#\" onclick=\"GetObjectsById('" + tl.query + "')\">" + tl.Name + "</a>";
                n.Text = "&nbsp;" + tl.Name;
                n.Value = tl.query;
                GetManyNode_TN_category(n, tl.Id, query);
                node.ChildNodes.Add(n);
            }
        }
        catch { }
    }
    #endregion
    #region  data1
    private void Treeview_data1()
    {
        string query = QueryHelper.Get_Value(Request, 1) == "" ? "/trang-chu" : QueryHelper.Get_Value(Request, 1);
        ICollection<data1> list_TaiLieu_download = data1DB.GetChildrenByQueryParent(false, query, 0);
        foreach (data1 item in list_TaiLieu_download)
        {
            TreeNode node = new TreeNode();
            node.Text = "&nbsp;" + item.Name;
            //string temp = path + dir.Name + "\\";
            node.Value = item.query;//this.ReplacePath(path) + dir.Name + "/";
            this.GetManyNode_Data1(node, item.Id, query);
            this.TreeView1.Nodes.Add(node);

        }
    }
    protected void GetManyNode_Data1(TreeNode node, int parent, string query)
    {
        try
        {
            List<data1> tls = data1DB.GetChildrenByQueryParent(false, query, parent).ToList<data1>();
            foreach (data1 tl in tls)
            {
                TreeNode n = new TreeNode();
                n.Text = "&nbsp;" + tl.Name;
                n.Value = tl.query;
                node.ChildNodes.Add(n);
            }
        }
        catch { }
    }
    #endregion
    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        string temp = this.TreeView1.SelectedNode.Value;
        Response.Redirect("?" + temp);
    }
}